﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using Model;

namespace Project
{
    public partial class Form1 : Form
    {
        #region 变量

        /// <summary>
        /// 行
        /// </summary>
        int row = CommonMethod.row;

        /// <summary>
        /// 列
        /// </summary>
        int column = CommonMethod.column;

        /// <summary>
        /// 雷数
        /// </summary>
        int landMineCount = 10;

        /// <summary>
        /// 方格集合
        /// </summary>
        List<Landmine> lstgird = new List<Landmine>();

        /// <summary>
        /// 地雷集合
        /// </summary>
        List<Landmine> lstLandMine = new List<Landmine>();

        /// <summary>
        /// 已经打开的方格集合
        /// </summary>
        List<Point> lstOpenedGirds = new List<Point>();

        #endregion
       
        public Form1()
        {
            InitializeComponent();
            CreateMinefields();
        }

        #region 事件

        void ld_Click(object sender, EventArgs e)
        {
            Landmine emptyLand = sender as Landmine;
            if (emptyLand.GridStyle == Style.Empty)
            {
                openEmptyGrid(emptyLand);
            }
            else if (emptyLand.GridStyle == Style.Landmine)
            {
                OpenAll();
            }
        }

        #endregion

        #region 方法

        /// <summary>
        /// 生成格子
        /// </summary>
        public void CreateMinefields()
        {
            int no = 1;
            for (int r = 0; r < row; r++)
            {
                for (int c = 0; c < column; c++)
                {
                    Landmine ld = new Landmine();
                    ld.GridNo = no++;
                    ld.Position = new Point(r, c);
                    lstgird.Add(ld);
                    this.panel1.Controls.Add(ld);
                    ld.Location = new Point(r * 20, c * 20);
                    ld.Click += new EventHandler(ld_Click);
                }
            }
            //生成地雷
            CreateLandMine();
        }

        /// <summary>
        /// 生成地雷
        /// </summary>
        public void CreateLandMine()
        {
            Random rd = new Random();
            string nos = string.Empty;//记录地雷所在方格编号.
            for (int i = 0; i < landMineCount; i++)
            {
                int no = 0;
                do
                {
                    no = rd.Next(0, row * column);
                    if (!nos.Split(',').Contains(no.ToString()))
                    {
                        nos += no.ToString() + ",";
                        break;
                    }
                } while (true);
            }

            foreach (Landmine grid in lstgird)
            {
                if (nos.Split(',').Contains(grid.GridNo.ToString()) && grid.GridStyle != Style.Landmine)
                {
                    grid.GridStyle = Style.Landmine;
                    lstLandMine.Add(grid);
                    SetNumber(grid);//生成地雷同时为地雷周围写数字.
                }
            }
        }

        public string GetLandMineCount(Point pt)
        {
            int landmineCount = 0;
            if ((pt - new Size(1, 1)).X >= 0 && (pt - new Size(1, 1)).Y >= 0)
            {
                if (lstLandMine.FirstOrDefault(p => p.Position == pt).GridStyle == Style.Landmine)
                {
                    landmineCount++;
                }
            }
            return "";
        }

        /// <summary>
        /// 绘制数字.
        /// </summary>
        /// <param name="grid"></param>
        public void SetNumber(Landmine grid)
        {
            List<Point> numberLst = CommonMethod.Return9Grid(grid);

            foreach (Landmine item in lstgird)
            {
                if (numberLst.Contains(item.Position))
                {
                    if (item.GridStyle != Style.Landmine)
                    {
                        item.GridStyle = Style.Number;
                    }
                }
            }
        }

        /// <summary>
        /// 打开空白格子.
        /// </summary>
        /// <param name="grid"></param>
        public void openEmptyGrid(Landmine grid)
        {
            List<Point> openList = CommonMethod.Return9Grid(grid);
            foreach (Landmine item in lstgird)
            {
                if (openList.Contains(item.Position))
                {
                    if (item.GridStatus == GridStatus.Close)
                    {
                        if (item.GridStyle == Style.Empty && !lstOpenedGirds.Contains(item.Position))
                        {
                            lstOpenedGirds.Add(item.Position);
                            openEmptyGrid(item);
                        }
                        item.GridStatus = GridStatus.Open;
                    }
                }
            }
        }

        /// <summary>
        /// 全部打开
        /// </summary>
        public void OpenAll()
        {
            foreach (Landmine item in lstgird)
            {
                if (item.GridStatus != GridStatus.Open)
                {
                    item.GridStatus = GridStatus.Open;
                }
            }
        }

        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            lstgird = new List<Landmine>();
            lstLandMine = new List<Landmine>();
            lstOpenedGirds = new List<Point>();
            this.panel1.Controls.Clear();
            CreateMinefields();
        }
    }
}
